package com.hfh.main.common.method;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 获取 某天到某天的所有天的集合
 */
public class DateRangeGenerator {
    /**
     * 获取 某天到今天的所有天
     */
    public static List<LocalDate> getDaysFromToNow(String startDateStr) {
        return generateDatesFromTo(startDateStr, String.valueOf(LocalDate.now()));
    };

    /**
     * 获取某天之后7天的日期
     */
    public static List<LocalDate> get7Days(String startDateStr) {
        LocalDate startDate = LocalDate.parse(startDateStr);
        return generateDatesFromTo(startDateStr, String.valueOf(startDate.plusDays(6)));
    };

    /**
     * 获取某天之后 第 hitCount个7天 的日期集合
     * @param startDateStr
     * @param hitCount 从0开始 是第一个7天
     * @return
     */
    public static List<LocalDate> getPartition7Days(String startDateStr,int hitCount) {
        LocalDate startDate = LocalDate.parse(startDateStr);
        return generateDatesFromTo(String.valueOf(startDate.plusDays(7*hitCount)), String.valueOf(startDate.plusDays(7*(hitCount+1)-1)));
    };

    public static List<LocalDate> generateDatesFromTo(String startDateStr,String endDateStr) {
        // 定义日期格式器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate startDate = LocalDate.parse(startDateStr, formatter);

        LocalDate endDate = LocalDate.parse(endDateStr, formatter);

        List<LocalDate> datesInRange = new ArrayList<>();

        // 循环遍历从开始日期到结束日期之间的每一天
        while (!startDate.isAfter(endDate)) {
            datesInRange.add(startDate);
            startDate = startDate.plusDays(1); // 增加一天
        }

        return datesInRange;
    }

    public static void main(String[] args) {
        // 调用函数并打印结果
        List<LocalDate> dates = generateDatesFromTo("2024-01-01","2024-07-01");
        List<LocalDate> dates_1 = get7Days("2024-01-01");
        List<LocalDate> dates_2 = getDaysFromToNow("2024-01-01");
        List<LocalDate> dates_3 = getPartition7Days("2024-07-01",2);
//        for (LocalDate date : dates) {
            System.out.println(dates);
//        }
    }
}
